/**
 * 
 */
package com.gosophia.conference.action.starting;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.commons.web.logAnnotation.LogRecord;
import com.gosophia.conferenceManagement.ConferenceTurningAction;
import com.gosophia.conferenceManagement.common.dto.ConferenceJobHandleDTO;
import com.gosophia.conferenceManagement.common.ws.ConferenceHandleWebService;
import com.gosophia.conferenceManagement.starting.dto.ConferenceApplicationDTO;
import com.gosophia.oa.action.JudgeCurrentUndertaker;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.opensymphony.xwork2.ActionContext;

/**
 * 功能：会议申请的审批
 * 
 * @author tianchunyu
 * @since 2011-3-22 Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Controller("ConferenceAppHandleAction")
@Scope("prototype")
public class ConferenceAppHandleAction extends BaseSupportAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	@Qualifier("conferenceHandleWebService")
	private ConferenceHandleWebService conferenceHandleWebService;
	
	@Autowired
	private JudgeCurrentUndertaker judgeCurrentUndertaker;

	private ConferenceJobHandleDTO conferenceJobHandleDTO;
	
	private ConferenceApplicationDTO conferenceApplicationDTO;
     
	private final Logger logger = Logger.getLogger(this.getClass()); 
	
	@Override
	@LogRecord(eventAction="会议管理-对会议申请的审批操作",eventTarget="conferenceJobHandleDTO")
	public String _execute() throws Exception {
		// 得到当前用户
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		
		judgeCurrentUndertaker.judgeOperationAuthority(conferenceJobHandleDTO.getJobId());

		Long userInfoId = userDetails.getUserAccountDTO().getUserInfoDTO()
				.getUserInfoId();
		
		conferenceJobHandleDTO.setOperatorId(userInfoId);
		logger.debug("进入会议申请处理conferenceJobHandleDTO： "+conferenceJobHandleDTO.toString());
		//处理有两种
		//提交审批【取回并提交】：需要处理applicationJobData
		//其他处理
		
		//logger.debug("进入会议申请处理conferenceApplicationDTO： "+conferenceApplicationDTO.toString());
		
		String message = "";
		if(conferenceJobHandleDTO.getHandleAction().equals(HandlePageCommand.GETBACK_APPROVAL)
		        ||conferenceJobHandleDTO.getHandleAction().equals(HandlePageCommand.MODIFY_SUBMIT_APPROVAL) ){
		    conferenceJobHandleDTO.setHandleAction(validHandleCommand(conferenceJobHandleDTO.getHandleAction()));
		    handleGetBackAndSubmitNext(conferenceJobHandleDTO,conferenceApplicationDTO);//修改申请，并提交申请
		    message = "提交审批成功";
		}else{
		    conferenceJobHandleDTO.setHandleAction(validHandleCommand(conferenceJobHandleDTO.getHandleAction()));
		    conferenceHandleWebService.handleConferenceJob(conferenceJobHandleDTO);
		    if(conferenceJobHandleDTO.getHandleAction().equals(HandlePageCommand.AGREE_FINALIZE)){
		    	message = "批准成功";
		    }else if(conferenceJobHandleDTO.getHandleAction().equals(HandlePageCommand.SENDBACK)){
		    	message = "退回承办人成功";
		    }else{
		    	message = "同意并提交成功";
		    }
		    
		}
		
		super.getRequestResult().setMessage(message);
		super.getRequestResult().setRsltCode("0");
		ActionContext.getContext().getSession().put("_lastOpResult",
				super.getRequestResult());
		
		
		if ("task".equals(this.getFromFlag())) {
			logger.debug("进入会议申请处理 成功 task");
			return "task";
		} else {
			logger.debug("进入会议申请处理 成功 business");
			return "business";
		}
	}

	//提交审批【取回并提交】
	private void handleGetBackAndSubmitNext(ConferenceJobHandleDTO conferenceJobHandleDTO,ConferenceApplicationDTO conferenceApplicationDTO) throws BusinessException{
	    conferenceHandleWebService.handleConferenceAppJob(conferenceApplicationDTO, conferenceJobHandleDTO);
	}
	
	private String  validHandleCommand(String command){
	     String transitionName= null; 
	    if(command.equals(HandlePageCommand.AGREE_FINALIZE)){
	        transitionName=ConferenceTurningAction.AGREE_FINALIZE.getValue();
	     }
	    else if(command.equals(HandlePageCommand.FINALIZE)){
	        transitionName=ConferenceTurningAction.FINALIZE.getValue();
	    }
	    else if(command.equals(HandlePageCommand.GETBACK_APPROVAL)){
	        transitionName=ConferenceTurningAction.GETBACK_APPROVAL.getValue();
	    }
	    else if(command.equals(HandlePageCommand.SENDBACK)){
	        transitionName=ConferenceTurningAction.SENDBACK.getValue();
	    }
	    else if(command.equals(HandlePageCommand.SUBMIT_NEXT_APPROVAL)){
	        transitionName=ConferenceTurningAction.SUBMIT_NEXT_APPROVAL.getValue();
	    }
	    else if(command.equals(HandlePageCommand.SUBMIT_APPROVAL)){
	        transitionName=ConferenceTurningAction.SUBMIT_APPROVAL.getValue();
	    }
	    else if(command.equals(HandlePageCommand.MODIFY_SUBMIT_APPROVAL)){
	        transitionName=ConferenceTurningAction.MODIFY_SUBMIT_APPROVAL.getValue();
	    }
	    else{
	        
	    }
	              
	    logger.debug("pageHandleCommand 转化 transitionName： command：["+command+"] transitionName:["+transitionName+"]" );
	    return transitionName;   
	}
	public ConferenceJobHandleDTO getConferenceJobHandleDTO() {
		return conferenceJobHandleDTO;
	}

	public void setConferenceJobHandleDTO(
			ConferenceJobHandleDTO conferenceJobHandleDTO) {
		this.conferenceJobHandleDTO = conferenceJobHandleDTO;
	}

	public ConferenceApplicationDTO getConferenceApplicationDTO() {
		return conferenceApplicationDTO;
	}

	public void setConferenceApplicationDTO(
			ConferenceApplicationDTO conferenceApplicationDTO) {
		this.conferenceApplicationDTO = conferenceApplicationDTO;
	}
	
 
	
}
